using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace SPTraceViewUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                string sptvPath = args[0];
                string installPath = Path.GetDirectoryName(sptvPath);

                if (File.Exists(sptvPath))
                {
                    try
                    {
                        Mutex stopMutex = null;
                        bool createdNew = false;
                        int secWaiting = 0;
                        while (
                            !createdNew &&
                            secWaiting < 60)
                        {
                            Thread.Sleep(400);
                            stopMutex = new Mutex(true, "~MUTEX~SPTRACEVIEW", out createdNew);

                            try
                            {
                                if (!createdNew)
                                    createdNew = stopMutex.WaitOne(100, false);
                            }
                            catch (AbandonedMutexException)
                            { }

                            Thread.Sleep(500);
                            secWaiting++;
                        }

                        if (createdNew)
                        {
                            try
                            {
                                Updater updater = new Updater(installPath);
                                var node = updater.NewUpdatesAvailable();
                                if (node != null)
                                {
                                    updater.PrepareToUpdate();
                                    updater.Update(node);

                                    string updatedToVersion = node.Version.ToString();
                                    string releaseDate = node.ReleaseDate;

                                    ProcessStartInfo pi = new ProcessStartInfo(sptvPath, string.Format(";$#{0};$#{1};$#{2}", Application.ExecutablePath, updatedToVersion, releaseDate));
                                    Process.Start(pi);
                                }
                            }
                            finally
                            {
                                stopMutex.ReleaseMutex();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Update failed. Please try again.", "Error updating SPTraceView", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "Error updating SPTraceView", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
    }
}
